package com.richpay.landiprintdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.landicorp.android.eptapi.DeviceService;
import com.landicorp.android.eptapi.exception.ReloginException;
import com.landicorp.android.eptapi.exception.RequestException;
import com.landicorp.android.eptapi.exception.ServiceOccupiedException;
import com.landicorp.android.eptapi.exception.UnsupportMultiProcess;
import com.richpay.landiprintdemo.data.TransData;
import com.richpay.landiprintdemo.data.TransType;
import com.richpay.landiprintdemo.print.LanDiPrinter;
import com.richpay.landiprintdemo.utils.DateUtil;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void print(View view) {
        bindDeviceService();

        TransData transData = new TransData();
        transData.setAmount("100");
        transData.setMerchantName("丰付测试商户");
        transData.setBatchNo("000001");
        transData.setCardNo("6200000000001");
        transData.setTransType(TransType.SALE.getType());
        transData.setSignData("");
        transData.setOrderDate(DateUtil.getOrderDate());

        printTickets(transData, 1);
    }

    /**
     * 打印小票
     */
    private void printTickets(TransData transData, int type) {

        LanDiPrinter.getInstance(this).setListener(new LanDiPrinter.PrinterListener() {
            @Override
            public void onPrintStart() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // todo 这里更新 UI
                    }
                });
            }

            @Override
            public void onFinish() {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                    }
                });
            }

            @Override
            public void onError(final String s) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //todo 提示错误
                    }
                });


            }
        });
        LanDiPrinter.getInstance(this).printTicket(transData, type);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DeviceService.logout();//解绑服务
    }

    //绑定设备服务
    private void bindDeviceService() {
        try {
            DeviceService.login(this);
        } catch (RequestException e) {
            // Rebind after a few milliseconds,
            // If you want this application keep the right of the device service runOnUiThreadDelayed(new Runnable() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    bindDeviceService();
                }
            });
            e.printStackTrace();
        } catch (
                ServiceOccupiedException e)

        {
            e.printStackTrace();
        } catch (
                ReloginException e)

        {
            e.printStackTrace();
        } catch (
                UnsupportMultiProcess e)

        {
            e.printStackTrace();
        }
    }
}
